package student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        ArrayList<Student> stu = new ArrayList<>();
        while (true) {
            menu();
            Scanner scanner = new Scanner(System.in);
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    add(stu);
                    break;
                case 2:
                    del(stu);
                    break;
                case 3:
                    update(stu);
                    break;
                case 4:
                    point(stu);
                    break;
                case 5:
                    exit();
            }
        }
    }

    private static void menu() {
        System.out.println("---------------------------");
        System.out.println("欢迎来到学生管理系统");
        System.out.println("1、添加学生");
        System.out.println("2、删除学生");
        System.out.println("3、修改学生");
        System.out.println("4、查看所有学生");
        System.out.println("5、退出");
        System.out.print("请输入你的选择：");
    }

    private static void exit() {
        System.out.println("欢迎下次使用！");
        System.exit(0);
    }

    private static void point(ArrayList<Student> list) {
        if (!list.isEmpty()) {
            System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                System.out.print(student.getSid() + "\t\t");
                System.out.print(student.getName() + "\t\t");
                System.out.print(student.getAge() + "\t\t");
                System.out.println(student.getHome() + "\t\t");
            }
        } else {
            System.out.println("无数据！");
        }
    }

    private static void update(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        Student stu = new Student();
        System.out.print("请输入要修改的学生的学号：");
        int id = scanner.nextInt();

        if (!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSid() == id) {
                    System.out.print("请输入要修改的学生的姓名：");
                    String name = scanner.next();
                    System.out.print("请输入要修改的学生的年龄：");
                    int age = scanner.nextInt();
                    System.out.print("请输入要修改的学生的居住地：");
                    String home = scanner.next();
                    stu.setSid(id);
                    stu.setName(name);
                    stu.setAge(age);
                    stu.setHome(home);
                    list.set(i,stu);
                    System.out.println("修改成功！");
                    break;
                }
            }
            System.out.println("数据不存在！");
        }else {
            System.out.println("无数据！");
        }
    }

    private static void del(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        Student stu = new Student();
        System.out.print("请输入要删除的学生的学号：");
        int id = scanner.nextInt();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid() == id) {
                System.out.print("是否确认删除(1、确认 2、取消)：");
                int res = scanner.nextInt();
                if (res == 1) {
                    list.remove(list.get(i));
                    System.out.println("删除成功！");
                }
            } else {
                System.out.println("无数据！");
                continue;
            }
        }
    }

    private static void add(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        Student stu = new Student();
        System.out.print("请输入学生的学号：");
        int id = scanner.nextInt();

        if (!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                while (list.get(i).getSid() == id) {
                    System.out.print("该学生已存在！\n请重新输入学号：");
                    int newId = scanner.nextInt();
                    id = newId;
                }
            }
        }
        System.out.print("请输入学生的姓名：");
        String name = scanner.next();
        System.out.print("请输入学生的年龄：");
        int age = scanner.nextInt();
        System.out.print("请输入学生的居住地：");
        String home = scanner.next();
        stu.setSid(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setHome(home);
        list.add(stu);
        System.out.println("添加成功！");
    }
}
